package edu.gatech.app4args.resources;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.URL;

import javax.swing.ImageIcon;

/**
 * A helper class to handle loading of all resources.
 * 
 * @author Andrew Lunsford
 */
public class ResourceLoader {
	/** An array of all the node icons */
	private static final String[] nodeIcons = {Globals.nIcon_list,
												Globals.nIcon_map,
												Globals.nIcon_issue,
												Globals.nIcon_position,
												Globals.nIcon_argument,
												Globals.nIcon_plus,
												Globals.nIcon_minus,
												Globals.nIcon_decision,
												Globals.nIcon_reference,
												Globals.nIcon_note};
	/** An array of all slide icons */
	private static final String[] slideIcons = {Globals.sIcon_title,
												Globals.sIcon_standard,
												Globals.sIcon_twoContent};

	/**
	 * Loads an icon for a node type.
	 *
	 * @param nType the node type icon to load
	 * @return the ImageIcon for the node type
	 */
	public static ImageIcon nodeImage(int nType){
		return loadImage(nodeIcons[nType-1]);
	}

	/**
	 * Loads an icon for a slide type.
	 *
	 * @param slidetype the slide type icon to load
	 * @return the ImageIcon for the slide type
	 */
	public static ImageIcon slideIcon(int slidetype){
		return loadImage(slideIcons[slidetype-1]);
	}

	/**
	 * Used to load an image into an ImageIcon.
	 *
	 * @param image the image to load
	 * @return an ImageIcon loaded from the resources/images package
	 */
	public static ImageIcon loadImage(String image){
		return new ImageIcon(resourcePathAsURL("images/"+image));
	}

	/**
	 * Returns a resource path, or null if the path was invalid.
	 *
	 * @param rsrc the resource to load
	 * @return a URL to the resource file
	 */
	public static URL resourcePathAsURL(String rsrc) {
		URL fileURL = ResourceLoader.class.getResource(rsrc);
		if (fileURL != null) {
			return fileURL;
		} else {
			System.err.println("Couldn't find file: " + rsrc);
			return null;
		}
	}

	/**
	 * Loads a slide layout xml file from the resources/slides package into a
	 * String.
	 *
	 * @param rsrc the layout to load
	 * @return a String built form the rsrc file
	 * @throws IOException from reading the file
	 */
	public static String slideLayoutFile(String rsrc) throws IOException {
		BufferedReader reader = null;
		String out = "";
		try {
			reader = new BufferedReader(new InputStreamReader(ResourceLoader.class.getResourceAsStream(rsrc)));
			String line = null;
			do {
				line = reader.readLine();
				if (line != null) {
					out+=line;
				}
			} while (line != null);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
				}
			}
		}

		return out;
    }

	/**
	 * Deprecated
	 * Loads a resource into a String. There should be no classes using it
	 * anymore. Left here in case something is still using it and can be
	 * uncommented to un-break the program.
	 *
	 * @deprecated Use slideLayoutFile() instead
	 * @param rsrc the file to load
	 * @return a String built form the file
	 * @throws IOException
	 */
	/*
	public static String resourceToString(String rsrc) throws IOException{
		String filepath = ResourceLoader.class.getClassLoader().getResource("edu/gatech/app4args/resources/"+rsrc).getPath();
		System.err.println("RL> filepath: "+filepath);
		StringBuffer fileData = new StringBuffer(1000);
		BufferedReader reader = new BufferedReader(new FileReader(filepath));
		char[] buf = new char[1024];
		int numRead=0;
		while((numRead=reader.read(buf)) != -1){
			String readData = String.valueOf(buf, 0, numRead);
			fileData.append(readData);
			buf = new char[1024];
		}
		reader.close();
		return fileData.toString();
	}*/
}
